package com.oahcfly.chgame.core.actions;

import com.badlogic.gdx.math.Bezier;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction;

/**
 * 
 * 贝塞尔曲线动作
 * @author haocao
 *
 */
public class CHBezierToAction extends TemporalAction{

	private Bezier<Vector2> bezier;

	/**
	 *  设置贝塞尔曲线参数
	 * @param bezierParam
	 */
	public void setBezier(Bezier<Vector2> bezierParam) {
		bezier = bezierParam;
	}

	@Override
	protected void begin() {
		lastPos = bezier.points.get(0);
	}

	@Override
	protected void end() {
		lastPos = null;
	}

	// 上个坐标
	private Vector2 lastPos;
	@Override
	protected void update(float percent) {
		Vector2 out = new Vector2();
		bezier.valueAt(out, percent);
		// 更新坐标
		target.setPosition(out.x, out.y);
		// cache用于后面赋值
		Vector2 copyOut = out.cpy();
		// 计算当前坐标与上个坐标之间的角度，用于更新Actor的角度
		float degrees = out.sub(lastPos).angle();
		target.setRotation(degrees);
		// 赋值当前坐标
		lastPos = copyOut;
	}
}
